package com.gxa.service.commlife.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.entity.commlife.CouponManagement;
import com.gxa.mapper.commlife.CouponManagementMapper;
import com.gxa.service.commlife.CouponManagementService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:39:47
 */
@Service
public class CouponManagementServiceImpl extends ServiceImpl<CouponManagementMapper, CouponManagement> implements CouponManagementService {

    @Autowired
    private CouponManagementMapper couponManagementMapper;

    @Override
    public int addCouponManagement(CouponManagement couponManagement) {
        int insert = this.couponManagementMapper.insert(couponManagement);
        return insert;
    }

    @Override
    public Map<String,Object> queryList(CouponManagement couponManagement, Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashedMap();
        QueryWrapper<CouponManagement> queryWrapper = new QueryWrapper();
        queryWrapper.like("coupon_name",couponManagement.getCouponName());
        if ((couponManagement.getValidPeriod()!=null&&!couponManagement.getValidPeriod().equals("")) && couponManagement.getState()!=null){
            queryWrapper.eq("valid_period",couponManagement.getValidPeriod());
            queryWrapper.eq("state",couponManagement.getState());
        }else if (couponManagement.getValidPeriod()!=null&&!couponManagement.getValidPeriod().equals("")){
            queryWrapper.eq("valid_period",couponManagement.getValidPeriod());
        }else if (couponManagement.getState()!=null){
            queryWrapper.eq("state",couponManagement.getState());
        }
        Page<CouponManagement> page = PageHelper.startPage(pageNum,pageSize);
        List<CouponManagement> couponManagementList = this.couponManagementMapper.selectList(queryWrapper);
        PageInfo<CouponManagement> pageInfo = new PageInfo<>(couponManagementList);
        map.put("pageInfo",pageInfo);
        return map;
    }

    @Override
    public void updateCouponManagement(CouponManagement couponManagement) {
        this.couponManagementMapper.updateById(couponManagement);

    }

    @Override
    public void deleteCouponManagement(Integer id) {
           this.couponManagementMapper.deleteById(id);
    }

    @Override
    public CouponManagement queryById(int id) {
        CouponManagement couponManagement = this.couponManagementMapper.selectById(id);
        return couponManagement;
    }
}
